package com.threeti.mecool.core.domain.model.plan.pojo;

import org.codehaus.jackson.annotate.JsonBackReference;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.annotate.JsonManagedReference;

public class UserStep {
	private Long id;
	private Integer version;
	private Step step;
	private transient Long stepId;
	private UserJob userJob;
	private UserTask userTask;
	
	public boolean isFinished() {
		return userTask != null && userTask.getTaskData() != null;
	}
	
	public TaskAction getTaskAction() {
		return userTask.getTask().getAction();
	}
	
	public Step getNextStep() {
		if (!isFinished()) {
			return step;
		}
		return step.getNextStep();
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@JsonIgnore
	public Step getStep() {
		if (step == null && stepId != null) {
			step = userJob.getJob().findStepById(stepId);
		}
		return step;
	}

	public void setStep(Step step) {
		this.step = step;
	}

	@JsonBackReference
	public UserJob getUserJob() {
		return userJob;
	}

	public void setUserJob(UserJob userJob) {
		this.userJob = userJob;
	}

	@JsonManagedReference
	public UserTask getUserTask() {
		return userTask;
	}

	public void setUserTask(UserTask userTask) {
		this.userTask = userTask;
		userTask.setUserStep(this);
	}

	public Long getStepId() {
		if (stepId == null && step != null) {
			stepId = step.getId();
		}
		return stepId;
	}

	public void setStepId(Long stepId) {
		this.stepId = stepId;
	}

	public Integer getVersion() {
		return version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}

}
